package EGS.impl;
import java.util.Vector;
import java.util.concurrent.ExecutorService;

import javax.swing.JTable;

import org.dom4j.Document;
import org.dom4j.Element;

import LOGISTICS.c_material;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service100004Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 100004 按<拼板>工单获取相关物料信息
	 */
	public EGS_execute_service100004Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("100004")) {// 按<拼板>工单获取相关物料信息
				/**
				 * 1.按拼板工单获取子板工单 2.获取合并标记 ORD_WOBinDet 3.获取所有相关工单 4.开线程下载物料信息
				 */
				String lv2Key = "orderInfo";
				String orderNo = null;
				orderNo = Mroot.element("body").element(lv2Key).attribute("order_no").getValue();// iSN=modelNo
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				if (orderNo == null) {
					orderNo = "-1";
				}
				try {
					String[] pickOrder = null;
					Vector<String> inputs = new Vector<String>();
					isPanel = false;
					String oOrder = orderNo;
					Thread iTH = new Thread(new Runnable() {
						String orderNo = oOrder;
						@Override
						public void run() {
							String str = "select masterOrderNumber from vOrderList_new where panelOrderNumber='"
									+ orderNo + "' group by masterOrderNumber";
							try {
								JTable t = sql.select(str);
								if (t != null && t.getRowCount() > 0) {
									isPanel = true;
									for (int i = 0; i < t.getRowCount(); i++) {
										String iOrder = (String) t.getValueAt(i, 0);
										inputs.add(iOrder);
									}
								}
							} catch (Exception e) {
								;
							}
						}
					});

					Thread iTH1 = new Thread(new Runnable() {
						String orderNo = oOrder;
						@Override
						public void run() {
							String str = "select masterOrderNumber from vOrderList_new where masterOrderNumber='"
									+ orderNo + "' group by masterOrderNumber";
							try {
								JTable t = sql.select(str);
								if (t != null && t.getRowCount() > 0) {
									for (int i = 0; i < t.getRowCount(); i++) {
										String iOrder = (String) t.getValueAt(i, 0);
										inputs.add(iOrder);
									}
								}
							} catch (Exception e) {
								;
							}
						}
					});
					iTH.start();
					iTH1.start();

					iTH.join();
					if (isPanel == false) {
						iTH1.join();
					}

					if (inputs.isEmpty()) {
						throw new Exception("工单" + orderNo + "不存在");
					}

					pickOrder = new c_material(sql).getPickListOrders_mergeNo(inputs);
					if (pickOrder == null) {
						throw new Exception("WMS工单" + inputs + "不存在");
					}
					Vector<String> sonOrders = new Vector<String>();
					String strPickOrders = "";
					for (int i = 0; i < pickOrder.length; i++) {
						if (pickOrder[i] != null && pickOrder[i].length() > 0 && sonOrders.indexOf(pickOrder[i]) < 0) {
							sonOrders.add(pickOrder[i]);
							if (sonOrders.size() == 1) {
								strPickOrders = "'" + pickOrder[i] + "'";
							} else {
								strPickOrders = strPickOrders + ",'" + pickOrder[i] + "'";
							}
						}
					}
					strPickOrders = "(" + strPickOrders + ")";
					new c_material(sql).getPickResult_groups(strPickOrders);
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(1000041, "" + orderNo + "_" + iStation);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(1000040, orderNo + "_" + iStation + ":_" + str);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
